package cn.yhjz.generator.service.impl;

import cn.yhjz.common.Pager;
import cn.yhjz.common.util.IdUtil;
import cn.yhjz.platform.system.util.LoginUtil;
import cn.yhjz.generator.entity.SysGenModel;
import cn.yhjz.generator.entity.SysGenModelProp;
import cn.yhjz.generator.mapper.SysGenModelPropMapper;
import cn.yhjz.generator.util.SqlUtil;
import cn.yhjz.generator.vo.SysGenModelVo;
import cn.yhjz.generator.mapper.SysGenModelMapper;
import cn.yhjz.generator.service.ISysGenModelService;
import cn.hutool.core.collection.CollUtil;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = {Exception.class})
public class SysGenModelServiceImpl implements ISysGenModelService {

    @Autowired
    private SysGenModelMapper sysGenModelMapper;
    @Autowired
    private SysGenModelPropMapper sysGenModelPropMapper;


    @Override
    public Pager getPage(Long currentPage, Long pageSize, SysGenModel sysGenModel) {
        Pager pager = new Pager();
        Long count = sysGenModelMapper.getPageCount(sysGenModel);
        Long pageOffset = (currentPage - 1) * pageSize;
        List<SysGenModelVo> records = sysGenModelMapper.getPage(pageOffset, pageSize, sysGenModel);
        pager.setRows(records);
        pager.setCurrentPage(currentPage);
        pager.setPageSize(pageSize);
        pager.setTotalPageCount(count);
        return pager;
    }

    @Override
    public SysGenModel insert(SysGenModelVo sysGenModelVo) {
        SysGenModel sysGenModel = new SysGenModel();
        BeanUtils.copyProperties(sysGenModelVo, sysGenModel);
        int res = sysGenModelMapper.insert(sysGenModel);
        if (res > 0) {
            return sysGenModel;
        } else {
            return null;
        }
    }

    @Override
    public List<SysGenModel> select(SysGenModel sysGenModel) {
        return sysGenModelMapper.select(sysGenModel);
    }

    @Override
    public List<SysGenModelVo> selectVo(SysGenModel sysGenModel) {
        return sysGenModelMapper.selectVo(sysGenModel);
    }

    @Override
    public SysGenModel selectOne(SysGenModel sysGenModel) {
        return sysGenModelMapper.selectOne(sysGenModel);
    }

    @Override
    public SysGenModelVo selectOneVo(SysGenModel sysGenModel) {
        return sysGenModelMapper.selectOneVo(sysGenModel);
    }

    @Override
    public SysGenModelVo selectById(Long id) {
        return sysGenModelMapper.selectById(id);
    }

    @Override
    public int deleteById(Long id) {
        return sysGenModelMapper.deleteById(id);
    }

    @Override
    public int updateByIdWithNull(SysGenModel sysGenModel) {
        return sysGenModelMapper.updateByIdWithNull(sysGenModel);
    }

    @Override
    public int updateById(SysGenModelVo sysGenModelVo) {
        SysGenModel sysGenModel = new SysGenModel();
        BeanUtils.copyProperties(sysGenModelVo, sysGenModel);
        SysGenModelVo old = this.sysGenModelMapper.selectById(sysGenModelVo.getId());
        //先删除原来所有的属性
        this.sysGenModelPropMapper.deleteByModelCode(old.getModelCode());
        if (CollUtil.isNotEmpty(sysGenModelVo.getPropList())) {
            for (SysGenModelProp prop : sysGenModelVo.getPropList()) {
                prop.setId(null);
                prop.setModelCode(old.getModelCode());
                prop.setUpdatedBy(LoginUtil.getCurrentUser().getUserCode());
                prop.setUpdatedTime(new Date());
                prop.setCreatedBy(LoginUtil.getCurrentUser().getUserCode());
                prop.setCreatedTime(new Date());
                this.sysGenModelPropMapper.insert(prop);
            }
        }
        return sysGenModelMapper.updateById(sysGenModel);
    }

    @Override
    public int update(SysGenModel entity, SysGenModel param) {
        return sysGenModelMapper.update(entity, param);
    }

    @Override
    public int updateWithNull(SysGenModel entity, SysGenModel param) {
        return sysGenModelMapper.updateWithNull(entity, param);
    }

    @Override
    public int addFromSql(String sql) {
        SysGenModel model = new SysGenModel();
        String modelCode = IdUtil.generate();
        model.setModelCode(modelCode);
        //先解析
        Map<String, Object> resMap = SqlUtil.r(sql);
        String tableName = (String) resMap.get("tableName");
        String className = (String) resMap.get("className");
        model.setTableName(tableName);
        model.setClassName(className);
        List<SysGenModelProp> columns = (List<SysGenModelProp>) resMap.get("columns");
        Date now = new Date();
        for (SysGenModelProp prop : columns) {
            prop.setModelCode(modelCode);
            prop.setCreatedTime(now);
        }
        //先删除原来所有的属性
        this.sysGenModelPropMapper.deleteByModelCode(modelCode);
        for (SysGenModelProp prop : columns) {
            this.sysGenModelPropMapper.insert(prop);
        }
        this.sysGenModelMapper.insert(model);
        return 1;
    }
}
